//前台在在线留言业务
const onlineMsgModel = require("../models/onlineMsg.js");
//const moment = require("moment")
class frOnlineMsgController {
    //渲染页面
    static async showPage(req, res) {
        res.render("../views/front/onlineMsg");
    }
    //入库
    static async saveData(req, res) {
        //res.json(req.body)
        let { username, tel, email, content } = req.body;
        let onlineMsgObj = new onlineMsgModel({ username, tel, email, content });
        let error = onlineMsgObj.validateSync();
        if (error) {
            const errorRs = [];
            for (let attr in error.errors) {
                errorRs.push(`${onlineMsgModel.fields[attr]}:${error.errors[attr]}`)
            }
            res.send(errorRs.map((v) => { return "\n" + v }));
            res.end()
            return;
        } else {
            await onlineMsgObj.save();
            res.send("已留言")
        }
    }
}

module.exports = frOnlineMsgController